package com.wie.controller;

import com.wie.constant.MessageConstant;
import com.wie.domain.dto.CountryGdpDTO;
import com.wie.domain.dto.GdpDataDTO;
import com.wie.domain.dto.ProvinceGdpDTO;
import com.wie.domain.entity.Result;
import com.wie.domain.vo.CountryGdpVO;
import com.wie.domain.vo.GdpDataVO;
import com.wie.domain.vo.ProvinceGdpVO;
import com.wie.service.CountryGdpService;
import com.wie.service.GdpDataService;
import com.wie.service.ProvinceGdpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.testng.reporters.AbstractXmlReporter;

@Slf4j
@RestController
@RequestMapping("/data")
@CrossOrigin(allowCredentials = "true", allowedHeaders = "true")
public class DataController {

    @Autowired
    private GdpDataService gdpDataService;
    @Autowired
    private ProvinceGdpService provinceGdpService;
    @Autowired
    private CountryGdpService countryGdpService;

    /**
     * 获取某一年的gdp
     * @param gdpDataDTO
     * @return
     */
    @GetMapping("/gdp")
    @ApiOperation("查询某年中国gdp")
    public Result<GdpDataVO> gdpData(GdpDataDTO gdpDataDTO) {
        log.info("查询中国GDP: {}", gdpDataDTO);
        if(gdpDataDTO.getYear() == null) {
            return Result.error(MessageConstant.INVALID_REQUEST);
        }
        GdpDataVO gdpDataVO = gdpDataService.getGdpData(gdpDataDTO);
        return Result.success(gdpDataVO);
    }

    /**
     * 获取某一年某省份的gdp
     * @param provinceGdpDTO
     * @return
     */
    @PostMapping("/provinceGDP")
    @ApiOperation("查询某一年某省份的gdp")
    public Result<ProvinceGdpVO> provinceGdpData(@RequestBody ProvinceGdpDTO provinceGdpDTO) {
        log.info("查询省GDP: {}", provinceGdpDTO);
        if(provinceGdpDTO.getYear() == null || provinceGdpDTO.getProvince() == null) {
            return Result.error(MessageConstant.INVALID_REQUEST);
        }
        ProvinceGdpVO provinceGdpVO = provinceGdpService.getProvinceGdpData(provinceGdpDTO);
        return Result.success(provinceGdpVO);
    }

    /**
     * 获取某一年某国家的gdp
     * @param countryGdpDTO
     * @return
     */
    @PostMapping("/countryGDP")
    @ApiOperation("获取某一年某国家的gdp")
    public Result<CountryGdpVO> countryGdpData(@RequestBody CountryGdpDTO countryGdpDTO) {
        log.info("查询国家GDP: {}", countryGdpDTO);
        if(countryGdpDTO.getYear() == null || countryGdpDTO.getCountry() == null) {
            return Result.error(MessageConstant.INVALID_REQUEST);
        }
        CountryGdpVO countryGdpVO = countryGdpService.getCountryGdpData(countryGdpDTO);
        return Result.success(countryGdpVO);
    }

}
